package controllers

import (
	"fmt"
	"github.com/goravel/framework/contracts/http"
	httpSwagger "github.com/swaggo/http-swagger"
	_ "goravel/docs"
	"net/url"
)

type SwaggerController struct {
}

func NewSwaggerController() *SwaggerController {
	return &SwaggerController{}
}

func (r *SwaggerController) Index(ctx http.Context) http.Response {
	uri, _ := url.Parse("http://localhost:3000/api")
	handler := httpSwagger.Handler(
		httpSwagger.URL("http://localhost:3000/swagger/doc.json"),
		httpSwagger.UIConfig(map[string]string{
			"onComplete": fmt.Sprintf(`() => {
    window.ui.setScheme('%s');
    window.ui.setHost('%s');
    window.ui.setBasePath('%s');
  }`, uri.Scheme, uri.Host, uri.Path),
		}),
	)
	handler(ctx.Response().Writer(), ctx.Request().Origin())
	return nil
}
